package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.MenuDao;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service(interfaceClass = MenuManagerService.class)
@Transactional
public class MenuManagerServiceImpl implements MenuManagerService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public PageInfo<Menu> findPage(QueryPageBean queryPageBean) {

       if (!StringUtils.isEmpty(queryPageBean.getQueryString())){
           queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
       }

       PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
       List<Menu> page = menuDao.findByCondition(queryPageBean.getQueryString());

        PageInfo pageInfo = new PageInfo(page);
        return pageInfo;
    }

    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public void update(Menu menu) {
        menuDao.update(menu);
    }

    @Override
    public void deleteById(Integer id) {
        menuDao.deleteById(id);
    }
}
